//话题详情
var lotteryNew = (function () {
    return {
        lottery_id: "",//若当前是修改话题信息，则保持话题ID
        pageIndex: 1,//标示当前页面数
        status: -1,
        type: "1",//1业主端，2师傅端
        _bootbox: {},//遮罩弹窗的句柄
        startTime: "",//起始时间

        //初始化函数
        init: function () {
            var self = this;
            //重置赛选条件
            function reset(onlyReset) {//重置查询条件
                self.searchTicketID = "";
                self.searchTicketName = "";
                $("#searchTicketID").val("");
                $("#searchTicketName").val("");
                if (onlyReset)return;
                //self.getList();
            }

            momo.initPage();
            momo.addPageEvent(self);
            $(document).on("click", ".resetBtn", reset);
            //$(".routing").attr("data-route", "./lottery.html?type=" + momo.getURLElement("type"));
            $(".routing").attr("data-route", "lottery.html?type=" + momo.getURLElement("type"));// icely change

            
            // icely add（输入限制）
            momo.inputText('.name', 20);
            momo.inputText('.remark', 150);
            momo.inputText('.lotteryName', 20);
            momo.inputText('.lotteryNum', 8);

            //$('.ticketName input').attr('min', '0').attr( 'max', '99999999').css('width', '90%');
            $('.lotteryNum').attr('min', '0').attr( 'max', '99999999').css('width', '90%');
            // 监听显示 得到焦点时，可能修改内容，全选内容
            $('.lotteryName').focus(function( event ){
                event.target.select();
            });

            // 监听库存 失去焦点时，值是否为非负整数（负数？小数？）
            $('.lotteryNum').change( function(){
                if( parseInt( $(this).val() ) < 0  ||  parseInt( $(this).val() ) != $(this).val() ){
                    alert('库存只能输入0和正整数！');
                    $(this).focus().select();
                }
            });

            $('.lotteryRand').change( function(){
                if( parseInt( $(this).val() ) > 10000 || parseInt( $(this).val() ) < 0  ||  parseInt( $(this).val() ) != $(this).val() ){
                    alert('概率只能输入0～10000的整数！');
                    $(this).focus().select();
                }
            });

            //点击发布
            $(document).on("click", ".submitBtn", this.addItem.bind(this));

            //取消新建
            $(document).on("click", ".cancelBtn2", function () {location.href = "./lottery.html";});

            //监听计算各种费用
//            $(document).on("click", ".daterangepicker .btn-success", self.calculateMoney.bind(self));//修改日期
//            $(document).on("input", ".lotteryNum, #joinStartTime ,#joinEndTime ,#joinPeriod", self.calculateMoney.bind(self));//修改数量重新计算
        },

        //添加话题//更新话题
        addItem: function () {
            var name = $(".name").val();
            var time = $(".time").val();
            var score = $(".score").val();
            var timeArr = time.split(' - ');
            if(timeArr.length <= 1){
                alert('请选择有效期');
                return;
            }
            var startTime = timeArr[0];
            var endTime = timeArr[1];
        //    var remark = $(".remark").val();
         //   var startTime = time.substr(0, 10);
         //   var endTime = time.substr(13, time.length);
            var pirzeList = [];//奖品对象数组
            if (score <= 0) {
                alert("请输入抽奖消耗积分");
                return;
            }
            var lotteryNameDom = $(".lotteryName");
            var lotteryNumDom = $(".lotteryNum");
            var lotteryRandDom = $(".lotteryRand");

            if(lotteryNameDom.length<3){
                alert("需要录入3个以上奖品");
                return;
            }

            // var start_hour = $("#joinStartTime").val().replace(":00","");
            // var end_hour = $("#joinEndTime").val().replace(":00","");
            // var period = $("#joinPeriod").val().replace("小时","");

            // if (parseInt(end_hour) < (parseInt(start_hour) + 1)) {
            //     alert("参与结束时间必须大于参与开始时间");
            //     return;
            // }

            //遍历价格设置
            for (var i = 0; i < 8; i++) {
                var nameItem = lotteryNameDom.eq(i).val();
                var numItem = lotteryNumDom.eq(i).val();
                var randItem = lotteryRandDom.eq(i).val();
                if (nameItem == "") {
                    alert("请输入奖品内容");
                    lotteryNameDom.eq(i).focus().select();
                    return;
                }
                else if (numItem == "") {
                    alert("请输入奖品库存");
                    lotteryNumDom.eq(i).focus().select();
                    return;
                }else if( parseInt( numItem ) < 0  ||  parseInt( numItem ) != numItem ){

                    // 编辑页面，不能修改库存，所以不判断库存是否合法（新建页面，会跳入if执行）
                    if( !momo.getURLElement("id") ){
                        alert('库存只能输入0和正整数！');
                        lotteryNumDom.eq(i).focus().select();
                        return;
                    }

                }
                if( parseInt(randItem) < 0 || parseInt(randItem) > 10000 || parseInt(randItem) != randItem){
                    alert('奖品概率设置错误');
                    lotteryRandDom.eq(i).focus().select();
                    return;
                }
                pirzeList.push({name: nameItem, sku: numItem, rand:randItem});
            }

            if (name == ""){
                alert("请输入活动名称！");
                return;
            }
            if (time == ""){
                alert("请输入有效期！");
                return;
            }
            var body = {
                "name": name,
                "score":score,
                "start_time": startTime,
                "end_time": endTime,
                // "start_hour": start_hour,
                // "end_hour": end_hour,
                "prizes": pirzeList
            };
            momo.sendPost(body, "PLottery/add", function (data) {
                if (data.errcode == 0) {
                    alert("操作成功");
                    window.location.href = "./lottery.html";
                }else{
                    alert(data.errmsg);
                }
            });
        },
    }
})
();

var init = lotteryNew.init.bind(lotteryNew);